package com.atguigu.tingshu.live.api;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.live.config.LiveRoomUtil;
import com.atguigu.tingshu.model.live.FromUser;
import com.atguigu.tingshu.model.live.SocketMsg;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

/**
 * title:
 * author: zgl
 * date: 2024/3/5
 * description：
 */
@Controller
@ServerEndpoint("/api/websocket/{liveRoomId}/{token}")
public class WebSocketController {
    private static RedisTemplate redisTemplate;

   /* @Autowired
    public WebSocketController(RedisTemplate redisTemplate) {
        this.redisTemplate =redisTemplate;
    }*/

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
            WebSocketController.redisTemplate = redisTemplate;
//        this.redisTemplate = redisTemplate;
    }


    @OnOpen
    public void onOpen(Session session, @PathParam("liveRoomId") Long liveRoomId, @PathParam("token") String token) {
        //进入直播间
        UserInfoVo userInfoVo = (UserInfoVo) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        LiveRoomUtil.addUser(userInfoVo.getId(), liveRoomId, session);
        //发送进入直播间消息
        SocketMsg socketMsg = new SocketMsg();
        socketMsg.setMsgType(SocketMsg.MsgTypeEnum.JOIN_CHAT.getCode());
        socketMsg.setMsgContent(userInfoVo.getNickname() + "加入了直播间");
        socketMsg.setLiveRoomId(liveRoomId);
        socketMsg.setTime(LocalDateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        //发送者
        FromUser fromUser = new FromUser();
        fromUser.setUserId(userInfoVo.getId());
        fromUser.setNickname(userInfoVo.getNickname());
        fromUser.setAvatarUrl(userInfoVo.getAvatarUrl());
        socketMsg.setFromUser(fromUser);
//        LiveRoomUtil.sendMsg(JSON.toJSONString(socketMsg));
        redisTemplate.convertAndSend(RedisConstant.CHANNEL_KEY_PREFIX, JSON.toJSONString(socketMsg));
    }

    @OnClose
    public void onClose(Session session, @PathParam("liveRoomId") Long liveRoomId, @PathParam("token") String token) {
        //退出直播间
        UserInfoVo userInfoVo = (UserInfoVo) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        LiveRoomUtil.removeUser(userInfoVo.getId(), liveRoomId);
        //发送进入直播间消息
        SocketMsg socketMsg = new SocketMsg();
        socketMsg.setMsgType(SocketMsg.MsgTypeEnum.JOIN_CHAT.getCode());
        socketMsg.setMsgContent(userInfoVo.getNickname() + "退出了直播间");
        socketMsg.setLiveRoomId(liveRoomId);
        socketMsg.setTime(LocalDateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        FromUser fromUser = new FromUser();
        fromUser.setUserId(userInfoVo.getId());
        fromUser.setNickname(userInfoVo.getNickname());
        fromUser.setAvatarUrl(userInfoVo.getAvatarUrl());
        socketMsg.setFromUser(fromUser);
//        LiveRoomUtil.sendMsg(JSON.toJSONString(socketMsg));
        redisTemplate.convertAndSend(RedisConstant.CHANNEL_KEY_PREFIX, JSON.toJSONString(socketMsg));
    }

    @OnMessage
    public void onMessage(Session session, String msg) {
//        LiveRoomUtil.sendMsg(msg);
        redisTemplate.convertAndSend(RedisConstant.CHANNEL_KEY_PREFIX, msg);
    }


    @OnError
    public void onError(Session session, Throwable th) {
        System.out.println("连接错误" + th.getMessage());
    }
}
